<script setup>
import { ref } from 'vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { articleGetChannelList } from '@/api/article'
import { formatDate } from '@/utils/date'
import { ElMessageBox, ElDrawer, ElMessage } from 'element-plus'
import ArticleEdit from './components/ArticleEdit.vue'
import { articleDelete } from '@/api/article'
const loading = ref(false)
import {
  Check,
  Delete,
  Edit,
  Message,
  Search,
  Star,
} from '@element-plus/icons-vue'
const dataTable = ref([])
//编辑文章逻辑
const editBtn = (row) => {
  articleEdit.value.open(row)
  console.log(row);

}
const articleEdit = ref()
//删除文章逻辑
const delBtn = async (row) => {
  await ElMessageBox.confirm(
    '你确定要删除这一条文章吗?',
    '需知',
    {
      confirmButtonText: 'OK',
      cancelButtonText: 'Cancel',
      type: 'warning',
    }
  )
    .then(() => {
      ElMessage({
        type: 'success',
        message: 'Delete 成功',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: 'Delete 失败',
      })
    })
  await articleDelete(row.id)
  getChannelList()
}
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
const total = ref(0)

const getChannelList = async () => {
  loading.value = true
  const res = await articleGetChannelList(params.value)
  dataTable.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getChannelList()
//搜索逻辑
const onSearch = async () => {
  params.value.pagenum = 1
  getChannelList()
}
//重置逻辑
const onReset = async () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getChannelList()
}
const handleSizeChange = (newSize) => {
  params.pagesize = newSize
  getChannelList()
}
const handleCurrentChange = (newvalue) => {
  params.pagenum = newvalue
  getChannelList()
}
//增加文章逻辑
const addArticle = () => {
  articleEdit.value.open({})
}
//发表文章成功后回显
const successAdd = (type) => {
  if (type === 'add') {
    params.value.pagenum = Math.ceil((total.value + 1) / params.value.pagesize)
    getChannelList()
  }
  else {
    getChannelList()
  }
}
</script>


<template>
  <pageContainer title="文章管理">
    <template #addButton>
      <el-button type="primary" @click="addArticle">添加文章</el-button>
    </template>
    <div class="form">
      <el-form inline label-width="100px" class="form-container">
        <el-form-item label="文章分类">
          <ChannelSelect v-model="params.cate_id"></ChannelSelect>
        </el-form-item>
        <el-form-item label="发布状态">
          <el-select placeholder="请选择状态" style="width: 200px;" v-model=params.state>
            <el-option label="已发布" value="已发布"></el-option>
            <el-option label="草稿" value="草稿"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">搜索</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table">
      <el-table :data="dataTable" v-loading="loading">
        <el-table-column label="文章标题" prop="title">
          <template #default="{ row, $index }">
            <el-link>{{ row.title }}</el-link>
          </template>

        </el-table-column>
        <el-table-column label="分类" prop="cate_name">

        </el-table-column>
        <el-table-column label="发表时间" prop="pub_date">
          <template #default="{ row }">
            {{ formatDate(row.pub_date) }}

          </template>
        </el-table-column>
        <el-table-column label="状态" prop="state">

        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row, $index }">
            <el-button type="primary" :icon="Edit" plain circle @click="editBtn(row)" />
            <el-button type="danger" :icon="Delete" plain circle @click="delBtn(row)" />
          </template>


        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination v-model:current-page="params.pagenum" v-model:page-size="params.pagesize"
        :page-sizes="[1, 2, 3, 5, 10]" :background="true" layout="jumper, prev, pager, next, sizes,total" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />

    </div>
    <!-- 抽屉组件 -->
    <ArticleEdit ref="articleEdit" @success="successAdd"></ArticleEdit>
  </pageContainer>
</template>
<style lang="scss" scoped>
.form-container {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.table {
  margin-top: 20px;
  border: 1px solid #ebeef5;
  border-radius: 5px;
  overflow: hidden;
  min-height: 300px;
  /* 设置最小高度，防止数据改变时页面晃动 */
}

.page {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px 0;
  background-color: #f9f9f9;
  border-top: 1px solid #ebeef5;
  border-radius: 0 0 5px 5px;
}

.el-pagination {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>